package com.project.decoration.controller.search;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationSearch;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 搜索词controller
 * @date 2025/02/12 11:11
 */
@Controller
@ResponseBody
@RequestMapping("search")
public class DecorationSearchController {

    @Autowired
    private DecorationSearchService decorationSearchService;

    /** 分页获取搜索词 */
    @PostMapping("getDecorationSearchPage")
    public Result getDecorationSearchPage(@RequestBody DecorationSearch decorationSearch) {
        Page<DecorationSearch> page = new Page<>(decorationSearch.getPageNumber(),decorationSearch.getPageSize());
        QueryWrapper<DecorationSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationSearch.getContent()),DecorationSearch::getContent,decorationSearch.getContent());
        Page<DecorationSearch> decorationSearchPage = decorationSearchService.page(page, queryWrapper);
        return Result.success(decorationSearchPage);
    }

    @GetMapping("getDecorationSearchList")
    public Result getDecorationSearchList() {
        QueryWrapper<DecorationSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().last("limit 5");
        List<DecorationSearch> searchList = decorationSearchService.list(queryWrapper);
        return Result.success(searchList);
    }

    /** 根据id获取搜索词 */
    @GetMapping("getDecorationSearchById")
    public Result getDecorationSearchById(@RequestParam("id")String id) {
        DecorationSearch decorationSearch = decorationSearchService.getById(id);
        return Result.success(decorationSearch);
    }

    /** 保存搜索词 */
    @PostMapping("saveDecorationSearch")
    public Result saveDecorationSearch(@RequestBody DecorationSearch decorationSearch) {
        boolean save = decorationSearchService.save(decorationSearch);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑搜索词 */
    @PostMapping("editDecorationSearch")
    public Result editDecorationSearch(@RequestBody DecorationSearch decorationSearch) {
        boolean save = decorationSearchService.updateById(decorationSearch);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除搜索词 */
    @GetMapping("removeDecorationSearch")
    public Result removeDecorationSearch(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationSearchService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("搜索词id不能为空！");
        }
    }

}
